package com.wyth.lottery.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wyth.lottery.R;
import com.wyth.lottery.activity.SSQSelectActivity;
import com.wyth.lottery.base.SaveUtils;
import com.wyth.lottery.bean.RandomBean;
import com.wyth.lottery.bean.SSQDataBean;
import com.wyth.lottery.bean.SSQDataBZBean;
import com.wyth.lottery.bean.SSQDataDTBean;
import com.wyth.lottery.bean.SSQSelectBean;
import com.wyth.lottery.utils.MyApplication;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by phh on 2017/11/2.
 * <p>
 * 双色球购物车列表的适配器
 */

public class ShoppingSSQAdapter extends RecyclerView.Adapter {
    private List<SSQDataBean> mList;
    private Activity mActivity;

    public ShoppingSSQAdapter(List<SSQDataBean> list, Activity Activity) {
        mList = list;
        mActivity = Activity;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.adapter_shopping_ssq, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MyViewHolder viewHolder = (MyViewHolder) holder;
        final SSQDataBean ssqDataBean = mList.get(position);
        viewHolder.mNumber.setText(String.valueOf(ssqDataBean.getInt()));
        viewHolder.mType.setText(ssqDataBean.getType());

        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(7, StaggeredGridLayoutManager.VERTICAL);
        viewHolder.mRecyclerView.setLayoutManager(staggeredGridLayoutManager);

        List<SSQSelectBean> red = ssqDataBean.getRed();
        List<SSQSelectBean> mblue = ssqDataBean.getMblue();
        final ArrayList<SSQSelectBean> mlist = new ArrayList<>();
        for (SSQSelectBean ssqSelectBean : red) {
            mlist.add(new SSQSelectBean(ssqSelectBean.getText(), ssqSelectBean.selectBoolean, ssqSelectBean.isBoolean));
        }
        for (SSQSelectBean ssqSelectBean : mblue) {
            mlist.add(new SSQSelectBean(ssqSelectBean.getText(), ssqSelectBean.selectBoolean, ssqSelectBean.isBoolean));
        }
        ShoppingNumberAdapter shoppingNumberAdapter = new ShoppingNumberAdapter(mlist);
        viewHolder.mRecyclerView.setAdapter(shoppingNumberAdapter);


        viewHolder.mAmend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SSQDataBean ssqDataBean1 = mList.get(position);
                String type = ssqDataBean1.getType();
                if (type.equals("标准投注")) {
                    EventBus.getDefault().postSticky(new SSQDataBZBean(ssqDataBean1.getRed(), ssqDataBean1.getMblue(), ssqDataBean1.getMoney()));
                    mlist.remove(ssqDataBean1);
                    SaveUtils.getGoodsInfos().mlist.remove(ssqDataBean1);
                    SaveUtils.getGoodsInfos().mTvMoneyNumber -= ssqDataBean1.getInt();
                    SaveUtils.getGoodsInfos().mTvMoney -= ssqDataBean1.getMoney();
                    Intent intent=new Intent(MyApplication.getContext(), SSQSelectActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApplication.getContext().getApplicationContext().startActivity(intent);
                    mActivity.finish();
                } else if (type.equals("胆拖投注")) {
                    EventBus.getDefault().postSticky("DT");
                    EventBus.getDefault().postSticky(new SSQDataDTBean(ssqDataBean1.getRed(), ssqDataBean1.getMblue(), ssqDataBean1.getMoney()));

                    Intent intent=new Intent(MyApplication.getContext(), SSQSelectActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApplication.getContext().startActivity(intent);
                    mlist.remove(ssqDataBean1);
                    SaveUtils.getGoodsInfos().mlist.remove(ssqDataBean1);
                    SaveUtils.getGoodsInfos().mTvMoneyNumber -= ssqDataBean1.getInt();
                    SaveUtils.getGoodsInfos().mTvMoney -= ssqDataBean1.getMoney();
                    mActivity.finish();
                }
            }
        });

        viewHolder.mDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SSQDataBean ssqDataBean1 = mList.get(position);
                mlist.remove(ssqDataBean1);
                SaveUtils.getGoodsInfos().mlist.remove(ssqDataBean1);
                SaveUtils.getGoodsInfos().mTvMoneyNumber -= ssqDataBean1.getInt();
                SaveUtils.getGoodsInfos().mTvMoney -= ssqDataBean1.getMoney();
                SaveUtils.getGoodsInfos().mTvMoneyNumberBei=1;
                EventBus.getDefault().postSticky(new RandomBean(20, 1));
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView mType;
        private final TextView mNumber;
        private final RecyclerView mRecyclerView;
        private final TextView mDelete;
        private final TextView mAmend;

        public MyViewHolder(View itemView) {
            super(itemView);

            mType = (TextView) itemView.findViewById(R.id.number_type);
            mNumber = (TextView) itemView.findViewById(R.id.tv_money_number);

            mRecyclerView = (RecyclerView) itemView.findViewById(R.id.rv);

            mDelete = (TextView) itemView.findViewById(R.id.delete);

            mAmend = (TextView) itemView.findViewById(R.id.amend);
        }
    }
}
